package written.test;

import edu.princeton.cs.algs4.In;

import java.util.*;

/**
 * @Author: Dang Qi
 * @Date: 2021/4/7  19:40
 * @Description:
 */
public class Huawei3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s0 = sc.nextLine().split(",");
        String[] s1 = sc.nextLine().split(",");
        int[] t = new int[s0.length];
        Deque<Integer> queue = new LinkedList<>();
        for(int i = 0; i < t.length; i++){
            t[i] = Integer.parseInt(s0[i]);
            queue.offer(i);
        }

        Map<Integer, Set<Integer>> map = new HashMap<>();
        for(int i = 0; i < s1.length; i++){
            String[] tmp = s1[i].split("->");
            int k = Integer.parseInt(tmp[0]);
            int v = Integer.parseInt(tmp[1]);
            Set<Integer> set = map.get(k);
            if(set==null){
                set = new HashSet<>();
            }
            set.add(v);
            map.put(k, set);
        }
        s0 = null; s1=null;
        cal(t, map, queue);
    }
    public static void cal(int[] t, Map<Integer, Set<Integer>> map, Deque<Integer> queue){
        int[] ans = new int[t.length];
        Set<Integer> success = new HashSet<>(t.length);
        int temp = 0;
        while (!queue.isEmpty()){
            Integer i = queue.poll();
            if(map.containsKey(i)){
                if(success.containsAll(map.get(i))){
                    temp += t[i];
                    ans[i] = temp;
                    success.add(i);
                }else{
                    queue.offer(i);
                }
            }else{
                success.add(i);
                temp += t[i];
                ans[i] = temp;
            }
        }
        System.out.print(ans[0]);
        for (int i = 1; i < ans.length; i++) {
            System.out.print(","+ans[i]);
        }
    }
}
